﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

public partial class Move_User_Booking : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string id = Session["pro_num"].ToString();
        LabPhoneNum.Text = Session["Move_Name"].ToString();
        LabName.Text = Session["move_username"].ToString();
        //当前时间及本周一时间获取
        DateTime dt = DateTime.Now;               //当前日期
        string DateNow = dt.ToString("yyyy-MM-dd");
        DateTime startWeek = dt.AddDays(1 - Convert.ToInt32(dt.DayOfWeek.ToString("d")));  //本周周一
        string DateStartWeek = startWeek.ToString("yyyy-MM-dd");

        string condition = "";

        //给本周的星期一到五赋值
        string Monday = "";
        string Tuesday = "";
        string Wednesday = "";
        string Thursday = "";
        string Friday = "";
        string Saturday = "";
        string Sunday = "";
        for (int i = 0; i < 7; i++)
        {
            DateTime newdt = startWeek.AddDays(i);
            switch (i)
            {
                case 0: Monday = newdt.ToString("yyyy-MM-dd"); break;
                case 1: Tuesday = newdt.ToString("yyyy-MM-dd"); break;
                case 2: Wednesday = newdt.ToString("yyyy-MM-dd"); break;
                case 3: Thursday = newdt.ToString("yyyy-MM-dd"); break;
                case 4: Friday = newdt.ToString("yyyy-MM-dd"); break;
                case 5: Saturday = newdt.ToString("yyyy-MM-dd"); break;
                case 6: Sunday = newdt.ToString("yyyy-MM-dd"); break;


                default:
                    break;
            }
        }

        SqlConnection conn = new SqlConnection();
        conn.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionStringProManage"].ConnectionString;
        try
        {
            conn.Open();
            string SqlGetCondition = "select condition from products where pro_num=@pro_num and flag=1";
            SqlCommand SqlCmdGetCondition = new SqlCommand();
            SqlCmdGetCondition.Connection = conn;
            SqlCmdGetCondition.CommandType = CommandType.Text;
            SqlCmdGetCondition.CommandText = SqlGetCondition;
            SqlParameter pGetCondition = new SqlParameter("@pro_num", id);
            SqlCmdGetCondition.Parameters.Add(pGetCondition);
            using (SqlDataReader reader = SqlCmdGetCondition.ExecuteReader())
            {
                while (reader.Read())
                {
                    condition = reader["condition"].ToString();
                }
            }
            if (condition != "正常")
            {
                Label11.Text = "暂停预约";
                Label21.Text = "暂停预约";
                Label31.Text = "暂停预约";
                Label41.Text = "暂停预约";
                Label51.Text = "暂停预约";
                Label61.Text = "暂停预约";
                Label71.Text = "暂停预约";

                Label12.Text = "暂停预约";
                Label22.Text = "暂停预约";
                Label32.Text = "暂停预约";
                Label42.Text = "暂停预约";
                Label52.Text = "暂停预约";
                Label62.Text = "暂停预约";
                Label72.Text = "暂停预约";

                Label13.Text = "暂停预约";
                Label23.Text = "暂停预约";
                Label33.Text = "暂停预约";
                Label43.Text = "暂停预约";
                Label53.Text = "暂停预约";
                Label63.Text = "暂停预约";
                Label73.Text = "暂停预约";

                Label14.Text = "暂停预约";
                Label24.Text = "暂停预约";
                Label34.Text = "暂停预约";
                Label44.Text = "暂停预约";
                Label54.Text = "暂停预约";
                Label64.Text = "暂停预约";
                Label74.Text = "暂停预约";

                Label15.Text = "暂停预约";
                Label25.Text = "暂停预约";
                Label35.Text = "暂停预约";
                Label45.Text = "暂停预约";
                Label55.Text = "暂停预约";
                Label65.Text = "暂停预约";
                Label75.Text = "暂停预约";

                Button1.Enabled = false;
            }
            else
            {
                string sqltable = "select date,time from booking where (pro_num=@pro_num and flag=1 and condition=N'预约成功')";
                SqlCommand cmdtable = new SqlCommand();
                cmdtable.Connection = conn;
                cmdtable.CommandType = CommandType.Text;
                cmdtable.CommandText = sqltable;
                SqlParameter ptable = new SqlParameter("@pro_num", id);
                cmdtable.Parameters.Add(ptable);
                using (SqlDataReader reader = cmdtable.ExecuteReader())
                {
                    //TableContent = "<table>< tr >< td ></ td >< td > 星期一 </ td >< td > 星期二 </ td >< td > 星期三 </ td >< td > 星期四 </ td >< td > 星期五 </ td >< td > 星期六 </ td >< td > 星期日 </ td ></ tr > ";
                    while (reader.Read())
                    {   //  reader [0]  为序数列的值，  “XH” 学号字段的值
                        string FlagTime = reader["time"].ToString();
                        string GetDate = reader["date"].ToString();
                        switch (FlagTime)
                        {
                            case "08:00-10:00":
                                if (GetDate == Monday)
                                {
                                    Label11.Text = "已预约";
                                    Label11.ForeColor = System.Drawing.Color.Red;
                                }
                                if (GetDate == Tuesday)
                                {
                                    Label21.Text = "已预约";
                                    Label21.ForeColor = System.Drawing.Color.Red;
                                }
                                if (GetDate == Wednesday)
                                {
                                    Label31.Text = "已预约";
                                    Label31.ForeColor = System.Drawing.Color.Red;
                                }
                                if (GetDate == Thursday)
                                {
                                    Label41.Text = "已预约";
                                    Label41.ForeColor = System.Drawing.Color.Red;
                                }
                                if (GetDate == Friday)
                                {
                                    Label51.Text = "已预约";
                                    Label51.ForeColor = System.Drawing.Color.Red;
                                }
                                if (GetDate == Saturday)
                                {
                                    Label61.Text = "已预约";
                                    Label61.ForeColor = System.Drawing.Color.Red;
                                }
                                if (GetDate == Sunday)
                                {
                                    Label71.Text = "已预约";
                                    Label71.ForeColor = System.Drawing.Color.Red;
                                }
                                break;
                            case "10:00-12:00":
                                if (GetDate == Monday)
                                {
                                    Label12.Text = "已预约";
                                    Label12.ForeColor = System.Drawing.Color.Red;
                                }
                                if (GetDate == Tuesday)
                                {
                                    Label22.Text = "已预约";
                                    Label22.ForeColor = System.Drawing.Color.Red;
                                }
                                if (GetDate == Wednesday)
                                {
                                    Label32.Text = "已预约";
                                    Label32.ForeColor = System.Drawing.Color.Red;
                                }
                                if (GetDate == Thursday)
                                {
                                    Label42.Text = "已预约";
                                    Label42.ForeColor = System.Drawing.Color.Red;
                                }
                                if (GetDate == Friday)
                                {
                                    Label52.Text = "已预约";
                                    Label52.ForeColor = System.Drawing.Color.Red;
                                }
                                if (GetDate == Saturday)
                                {
                                    Label62.Text = "已预约";
                                    Label62.ForeColor = System.Drawing.Color.Red;
                                }
                                if (GetDate == Sunday)
                                {
                                    Label72.Text = "已预约";
                                    Label72.ForeColor = System.Drawing.Color.Red;
                                }
                                break;
                            case "14:00-16:00":
                                if (GetDate == Monday)
                                {
                                    Label13.Text = "已预约";
                                    Label13.ForeColor = System.Drawing.Color.Red;
                                }
                                if (GetDate == Tuesday)
                                {
                                    Label23.Text = "已预约";
                                    Label23.ForeColor = System.Drawing.Color.Red;
                                }
                                if (GetDate == Wednesday)
                                {
                                    Label33.Text = "已预约";
                                    Label33.ForeColor = System.Drawing.Color.Red;
                                }
                                if (GetDate == Thursday)
                                {
                                    Label43.Text = "已预约";
                                    Label43.ForeColor = System.Drawing.Color.Red;
                                }
                                if (GetDate == Friday)
                                {
                                    Label53.Text = "已预约";
                                    Label53.ForeColor = System.Drawing.Color.Red;
                                }
                                if (GetDate == Saturday)
                                {
                                    Label63.Text = "已预约";
                                    Label63.ForeColor = System.Drawing.Color.Red;
                                }
                                if (GetDate == Sunday)
                                {
                                    Label73.Text = "已预约";
                                    Label73.ForeColor = System.Drawing.Color.Red;
                                }
                                break;
                            case "16:00-18:00":
                                if (GetDate == Monday)
                                {
                                    Label14.Text = "已预约";
                                    Label14.ForeColor = System.Drawing.Color.Red;
                                }
                                if (GetDate == Tuesday)
                                {
                                    Label24.Text = "已预约";
                                    Label24.ForeColor = System.Drawing.Color.Red;
                                }
                                if (GetDate == Wednesday)
                                {
                                    Label34.Text = "已预约";
                                    Label34.ForeColor = System.Drawing.Color.Red;
                                }
                                if (GetDate == Thursday)
                                {
                                    Label44.Text = "已预约";
                                    Label44.ForeColor = System.Drawing.Color.Red;
                                }
                                if (GetDate == Friday)
                                {
                                    Label54.Text = "已预约";
                                    Label54.ForeColor = System.Drawing.Color.Red;
                                }
                                if (GetDate == Saturday)
                                {
                                    Label64.Text = "已预约";
                                    Label64.ForeColor = System.Drawing.Color.Red;
                                }
                                if (GetDate == Sunday)
                                {
                                    Label74.Text = "已预约";
                                    Label74.ForeColor = System.Drawing.Color.Red;
                                }
                                break;
                            case "19:00-21:00":
                                if (GetDate == Monday)
                                {
                                    Label15.Text = "已预约";
                                    Label15.ForeColor = System.Drawing.Color.Red;
                                }
                                if (GetDate == Tuesday)
                                {
                                    Label25.Text = "已预约";
                                    Label25.ForeColor = System.Drawing.Color.Red;
                                }
                                if (GetDate == Wednesday)
                                {
                                    Label35.Text = "已预约";
                                    Label35.ForeColor = System.Drawing.Color.Red;
                                }
                                if (GetDate == Thursday)
                                {
                                    Label45.Text = "已预约";
                                    Label45.ForeColor = System.Drawing.Color.Red;
                                }
                                if (GetDate == Friday)
                                {
                                    Label55.Text = "已预约";
                                    Label55.ForeColor = System.Drawing.Color.Red;
                                }
                                if (GetDate == Saturday)
                                {
                                    Label65.Text = "已预约";
                                    Label65.ForeColor = System.Drawing.Color.Red;
                                }
                                if (GetDate == Sunday)
                                {
                                    Label75.Text = "已预约";
                                    Label75.ForeColor = System.Drawing.Color.Red;
                                }
                                break;


                            default:
                                break;
                        }
                        //attribute += reader["keyword"] + ":&nbsp;";
                        //attribute += reader["pro_value"] + "<br />";
                    }
                }



                //if (reader.Read()==false)
                //{
                //    TableContent = "暂还没有预约信息哦！";
                //}

            }
        }
        finally
        {
            conn.Close();
        }
    }

    protected void Button1_Click1(object sender, EventArgs e)
    {
        string name = LabName.Text;
        string phonenum = LabPhoneNum.Text;
        string identity = FProCateg.SelectedValue.Trim();
        string date = FDate.Text.Trim();
        string time = FTime.Text.Trim();
        string pronum = Session["pro_num"].ToString();
        string place = "";
        string remarks = FProRemarks.Text.Trim();

        SqlConnection conn = new SqlConnection();
        conn.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionStringProManage"].ConnectionString;
        try
        {
            conn.Open();
            //读仪器名称
            string SQLplace = "select pro_location from Products where pro_num=@pronum";
            SqlCommand cmdplace = new SqlCommand();
            cmdplace.Connection = conn;
            cmdplace.CommandType = CommandType.Text;
            cmdplace.CommandText = SQLplace;
            SqlParameter p1 = new SqlParameter("@pronum", pronum);
            cmdplace.Parameters.Add(p1);
            //cmdimg.ExecuteNonQuery();
            place = cmdplace.ExecuteScalar().ToString();

            string SQL = "insert into Booking (name,phone_num,user_identity,date,time,pro_num,pro_location,remarks) values (@name,@phone_num,@user_identity,@date,@time,@pro_num,@pro_location,@remarks)";
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = SQL;

            //依次添加每个字段的值
            SqlParameter pname = new SqlParameter("@name", SqlDbType.NVarChar, 10);
            pname.Value = name;
            cmd.Parameters.Add(pname);

            SqlParameter pnum = new SqlParameter("@phone_num", SqlDbType.VarChar, 11);
            pnum.Value = phonenum;
            cmd.Parameters.Add(pnum);

            SqlParameter piden = new SqlParameter("@user_identity", SqlDbType.NVarChar, 10);
            piden.Value = identity;
            cmd.Parameters.Add(piden);

            SqlParameter pdate = new SqlParameter("@date", SqlDbType.NVarChar, 20);
            pdate.Value = date;
            cmd.Parameters.Add(pdate);

            SqlParameter ptime = new SqlParameter("@time", SqlDbType.NVarChar, 20);
            ptime.Value = time;
            cmd.Parameters.Add(ptime);

            SqlParameter ppronum = new SqlParameter("@pro_num", SqlDbType.NVarChar, 10);
            ppronum.Value = pronum;
            cmd.Parameters.Add(ppronum);

            SqlParameter pplace = new SqlParameter("@pro_location", SqlDbType.NVarChar, 20);
            pplace.Value = place;
            cmd.Parameters.Add(pplace);

            SqlParameter premarks = new SqlParameter("@remarks", SqlDbType.NVarChar, 100);
            premarks.Value = remarks;
            cmd.Parameters.Add(premarks);

            int R = cmd.ExecuteNonQuery();  //R 的值就是返回影响表的行数
            if (R > 0)
            {
                Session["Message"] = "预约成功，等待管理员处理！";
                Session["ReturnPath"] = "Move_User_Booking.aspx";
                Response.Redirect("Success.aspx");

            }
            else
            {
                Session["Message"] = "预约失败！";
                Response.Redirect("Error.aspx");
            }

        }
        finally
        {
            conn.Close();
        }
    }
}